<?php
/**
 * Api 订单
 * @author zhaofengjun
 */

namespace app\api\controller;

use app\common\model\GoodsL;
use app\common\model\OrderDetails;
use app\common\model\Orders;
use think\Log;


class Order extends Api
{
    protected $autoWriteTimestamp = false;
    protected $needAuth = false;

    /**
     * 订单列表
     */
    public function orderList()
    {
        //当前页数
        $page = isset($this->param['page']) ? $this->param['page'] : 1;
        //每页数量
        $num  = isset($this->param['num']) ? $this->param['num'] : 10;

        //限制每页数量，防止恶意请求数据量过大
        $num = $num>100?100:$num;

        // 查询条件  ，如果有订单id 查询相关数据，如果没有查询所有非空
        $orderId = isset($this->param['order_id'])? $this->param['order_id'] : 'not null';

        $list = OrderDetails::with('goodsAll')
            ->where('order_id',$orderId)
            ->order('id', 'desc')
            ->page($page, $num)
            ->select();

        $listUser = Orders::with('gUser')
            ->where('id',$orderId)
            ->order('id', 'desc')
            ->page($page, $num)
            ->select();

        if ($list) {
            return $this->success(array_merge($list,$listUser));
        }
        return $this->error('获取数据为');
    }

    /**
     * 订单列表中的商品
     */
    public function goodsList()
    {
        //当前页数
        $page = isset($this->param['page']) ? $this->param['page'] : 1;
        //每页数量
        $num  = isset($this->param['num']) ? $this->param['num'] : 10;

        //限制每页数量，防止恶意请求数据量过大
        $num = $num>100?100:$num;

        // 查询条件  ，如果有订单id 查询相关数据，如果没有查询所有非空
        $goodsId = isset($this->param['goods_id'])? $this->param['goods_id'] : 'not null';
        $list = GoodsL::where('id',$goodsId)
            ->order('id', 'desc')
            ->page($page, $num)
            ->select();
        if ($list) {
            return $this->success($list);
        }
        return $this->error('获取数据为空');
    }

    /**
     * 订单列表中的商品  模糊查询
     */
    public function goodsByName()
    {
        // 查询条件  ，如果有订单id 查询相关数据，如果没有查询所有非空
        $goodsName = isset($this->param['name'])? $this->param['name'] : '';
        $adminId = isset($this->param['admin_uid'])? $this->param['admin_uid'] : 'not null';
        $list = GoodsL::where('name','like','%'.$goodsName.'%')
            ->where('admin_uid',$adminId)
            ->order('id', 'desc')
            ->select();
        if ($list) {
            return $this->success($list);
        }
        return $this->error('获取数据为空');
    }



    /**
     * 订单添加商品
     */
    public function add()
    {
        $result = OrderDetails::create($this->param);
        if ($result) {
            return $this->success($result);
        }
        return $this->error('新增数据失败');

    }

    /**
     * 修改订单商品
     */
    public function edit()
    {
        $model = new Orders();
        $model2 = new OrderDetails();
        $price = $this->param['price'];
        $goods = $this->param['goods_all'];
        if($this->param['price'] ==0.00){
            $price = $goods['advance_price'];
        }
        Log::info($price);
        // 计算商品价钱 ，小计
        $this->param['subtotal'] = $this->param['goods_num'] * $price;
        $result = $model2::update($this->param);
        if ($result) {
            // 更新订单总金额
            $money = $model2->where('order_id',$result['order_id'])
                ->sum('subtotal');
            $result2 = $model::update([
                'id' => $result['order_id'],
                'total' =>$money
            ]);
            if (!$result2) {
                return $this->error();
            }
            return $this->success($result);
        }
        return $this->error('新增数据失败');

    }

    /**
     * 获取打印列表
     */
    public function printList(){
        $model = new Orders();
        $goods_model = new GoodsL();
        $ids = $this->param['orderIds'];

        $list = Orders::with('orderDetails,gUser')
            ->where('id','in',$ids)
            ->select();

        foreach ($list as $k => $v){
            //Log::info('abc:'.$v);
            $orderDetailsList = $list[$k]['order_details'];
            foreach ($orderDetailsList as $oDetailsK => $oDetailsV){
                $orderDetailsList[$oDetailsK]['goods'] = $goods_model->where('id',$oDetailsV['goods_id'])->field('id,name,goods_spe')->find();
            }
        }

        if ($list) {
            return $this->success($list);
        }
        return $this->error('获取数据为空');


    }


}